home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 February: Tool Chest / Dev.CD Feb 94.toast / New System Software Extensions / QuickDraw™ GX v1.0ß2 / Sample Code / Graphics Samples / Patterned Curve ƒ / graphics shell.c next >
Encoding:
C/C++ Source or Header  |  1993-09-14  |  7.6 KB  |  255 lines  |  [TEXT/KAHL]

  1. /** 
  2.  --        Graphics Shell ("GX").c
  3.  --
  4.  --        This file is a shell that can be used to build "new" Graphics applications.  
  5.  --        It contains all of the required calls to use the "new" Graphics routines and 
  6.  --        QuickDraw (i.e. windows) together. It will put up one window. You "quit" 
  7.  --        the application by clicking in the close bix.  This shell does not use a menu.
  8.  --        
  9.  --     The application is expected to supply the following functions which are called
  10.  --        by this shell:
  11.  --
  12.  --            void DoInitialization(WindowPtr);
  13.  --            void DoDraw(WindowPtr);
  14.  --            void DoDispose(WindowPtr);
  15.  --            void DoClick(WindowPtr);
  16.  --            void DoIdle(WindowPtr);
  17.  --
  18.  --
  19.  --     Change History:         
  20.  --
  21.  --        3/90    New
  22.  --
  23.  --        6/91    Updated the shell to reflect the changes in "Graphics" v1.0d21.2.  PLA
  24.  --
  25.  --        6/92    Made the following variables global: gDebugging, gGiveMeValidation,gGraphicsHeapSize. 
  26.  --                See the comments for detail in this file.  PLA
  27.  --
  28.  --        8/93    - Updated this file to run with the QD GX ß2 "GX-ified" interfaces
  29.  --                - worked around a problem with GXConvertQDPoint (..) see the comments within
  30.  --                  the GetWindowBoundsShape (..) call for details (below).  PLA
  31.  --
  32.  --
  33.  --        © Apple Computer, Inc. 1990 - 1993  All rights reserved
  34.  --                                
  35. **/
  36.  
  37.  
  38. #include <Desk.h>
  39. #include <Events.h>
  40. #include <Fonts.h>
  41. #include <Windows.h>
  42. #include <Memory.h>
  43. #include <ToolUtils.h>
  44. #include <Quickdraw.h>
  45.  
  46. #include "graphics toolbox.h"
  47. #include "graphics routines.h"
  48. #include "graphics libraries.h"
  49. #include "graphics debugging.h"
  50. #include "graphics shell.h"
  51.  
  52. #define    kOSEvent                        app4Evt    /* event used by MultiFinder */
  53. #define    kSuspendResumeMessage            1        /* high byte of suspend/resume event message */
  54. #define    kResumeMask                        1        /* bit of message field for resume vs. suspend */
  55.  
  56.  
  57. WindowPtr         gWindow, whichWindow;
  58.  
  59. EventRecord        gtheEvent;
  60. gxShape         gWindowBoundsShape;
  61.  
  62.  
  63. /**------ Function Prototypes -----**/
  64. void main(void);
  65. gxShape GetWindowBoundsShape(void);
  66. Boolean EventLoop(void);
  67.  
  68.  
  69. /*------ main -----------------------------------------------------------------------------------------*/
  70.  
  71. void main()
  72. {        
  73.     gxGraphicsClient     client;
  74.     gxViewPort            theWindowsViewPort;    
  75.     CursHandle            theCurs; 
  76.  
  77.     /**     
  78.         The NewGraphicsClient routine defines the graphics heap size. If you do not make this call, 
  79.         the GX graphics engine will create this heap automatically. How? It will create a heap which 
  80.         is a percentage of your application's ideal memory foot print. This call allows you to explicity 
  81.         define the ammount of memory used by the graphics system for it's graphics objects heap.
  82.     **/
  83.     client = GXNewGraphicsClient(nil, gGraphicsHeapSize * 1024, 0L);
  84.  
  85.     /**   Generic heap initialization.  **/
  86.     MaxApplZone(); 
  87.     MoreMasters(); MoreMasters(); MoreMasters(); 
  88.     MoreMasters(); MoreMasters(); MoreMasters(); 
  89.  
  90.     /** 
  91.         If gDebugging = TRUE, you will receive graphics library errors & notices will be posted.  This
  92.         functionality will only work with the "debugging" version of the Secret Graphics init.  If this
  93.         init is not installed, these functions will not work. The "debugging" version of the Secret 
  94.         Graphics init is approximately 700K.
  95.     **/
  96.     if (gDebugging) {
  97.         SetGraphicsLibraryErrors ();
  98.         SetGraphicsLibraryNotices();    
  99.     }
  100.  
  101.     /** 
  102.         Set  "gGiveMeValidation" to TRUE, if you want run-time validation. As you increase the amount
  103.         of validation, The drawing speed will SLOW down due to all of the internal checking. 
  104.         
  105.         publicValidation will check parameters to public routines. For additional details regarding 
  106.         the various levels of validation, please see the documentation.
  107.     **/
  108.     if (gGiveMeValidation) GXSetValidation(gxPublicValidation + gxTypeValidation); 
  109.  
  110.  
  111.     /** initialize the new graphics environment **/
  112.     GXEnterGraphics();
  113.  
  114.     /**   initialize the toolbox   **/
  115.      InitGraf(&qd.thePort);
  116.     InitFonts();
  117.     InitWindows();
  118.     InitCursor();
  119.  
  120.     theCurs = GetCursor(watchCursor);
  121.     SetCursor(*theCurs);
  122.  
  123.     /**  Create a window and attach a default viewPort to it **/
  124.     gWindow = NewWindow(nil, &gWindowQDRect, gWindowTitle, true, noGrowDocProc, (WindowPtr)-1L, true, 0L);
  125.     theWindowsViewPort = GXNewWindowViewPort(gWindow);
  126.  
  127.     GXIgnoreGraphicsNotice(transform_already_set);
  128.     SetDefaultViewPort(theWindowsViewPort);
  129.     GXPopGraphicsNotice();
  130.     
  131.     /**  Get the global bounds of the window  **/
  132.     gWindowBoundsShape = GetWindowBoundsShape();
  133.  
  134.     DoInitialization(gWindow);
  135.  
  136.     SetCursor(&qd.arrow);  
  137.     
  138.     while (EventLoop())
  139.         DoIdle(gWindow);   /** loop until the window is closed  **/
  140.  
  141.     DoDispose(gWindow);
  142.     
  143.     GXExitGraphics();        /** Deallocate all of the default structures **/
  144.     GXDisposeGraphicsClient(client);
  145. }
  146.  
  147.  
  148.  
  149. /*------ GetWindowBoundsShape -------------------------------------------------------------------------*/
  150.  
  151. gxShape  GetWindowBoundsShape()
  152. {
  153.     Rect                theRect;
  154.     Point                QDtopLeft;
  155.     Point                QDbotRight;
  156.     gxPoint                QDGXtopLeft;
  157.     gxPoint                QDGXbotRight;
  158.     gxRectangle            theQDGXRect;
  159.     gxValidationLevel     currentValidation;
  160.     
  161.     /**  The QuickDraw rect and points which represent the portRect of the window.  **/
  162.     theRect = gWindow->portRect;
  163.     QDtopLeft.h = theRect.left;
  164.     QDtopLeft.v = theRect.top;
  165.     QDbotRight.h = theRect.right;
  166.     QDbotRight.v = theRect.bottom;
  167.     
  168.      //
  169.      //    Unfortunately, GXConvertQDPoint(..) will not survive validation with the QD GX ß2
  170.      //    seed. Therefore, we get the validation level set up by the application,  and turn the
  171.      //    validation off. We will reset the validation to it's original setting below.
  172.      //
  173.      //    This problem has already been fixed in the QD GX ß3 build, therefore this work around 
  174.      //    will be removed at ß3. Details about the parameters for the GXConvertQDPoint (..) call
  175.      //    can be found in the QD GX ß2 Graphics Notes within the "• Open Me First •" folder.
  176.      // 
  177.     currentValidation = GXGetValidation();
  178.       if (currentValidation) GXSetValidation(gxNoValidation);
  179.      
  180.     /**  Convert the global Quickdraw coordinates to local fixed coordinates.  **/
  181.     GXConvertQDPoint(&QDtopLeft, 0, &QDGXtopLeft);
  182.     GXConvertQDPoint(&QDbotRight, 0, &QDGXbotRight);
  183.  
  184.     //
  185.     //    Reset the validation to the original setting.
  186.     //
  187.        if (currentValidation) GXSetValidation(currentValidation);
  188.  
  189.     /**  Setup the dimensions for "gWindowBoundsShape" **/ 
  190.     theQDGXRect.top = QDGXtopLeft.y;
  191.     theQDGXRect.left = QDGXtopLeft.x;
  192.     theQDGXRect.bottom = QDGXbotRight.y;
  193.     theQDGXRect.right = QDGXbotRight.x;
  194.     
  195.     return (GXNewRectangle(&theQDGXRect));
  196. }
  197.  
  198.  
  199.  
  200. /*------ EventLoop ------------------------------------------------------------------------------------*/
  201.  
  202. Boolean EventLoop()
  203. {
  204.     static long     sleep = 0;
  205.  
  206.     WaitNextEvent(everyEvent, >heEvent, sleep, nil);
  207.          
  208.     switch(gtheEvent.what)
  209.     {                    
  210.         case updateEvt:
  211.                 BeginUpdate((WindowPtr) gtheEvent.message);
  212.                 SetPort(gWindow);
  213.                 DoDraw(gWindow);
  214.                 EndUpdate((WindowPtr) gtheEvent.message);
  215.         break;
  216.         
  217.         case mouseDown:
  218.             switch (FindWindow(gtheEvent.where, &whichWindow)) {
  219.                 case inSysWindow:
  220.                     SystemClick(>heEvent, whichWindow);
  221.                 break;
  222.                         
  223.                 case inDrag:
  224.                     DragWindow(whichWindow, gtheEvent.where, &qd.screenBits.bounds);
  225.                 break;
  226.  
  227.                 case inGoAway:
  228.                     if (TrackGoAway(whichWindow, gtheEvent.where))
  229.                         return false;
  230.                 break;
  231.                 
  232.                 case inContent:
  233.                     if (whichWindow != FrontWindow())
  234.                          SelectWindow(whichWindow);
  235.                      else
  236.                          DoClick(gWindow);
  237.                 break;
  238.  
  239.             }
  240.             
  241.         case kOSEvent:
  242.             switch ((unsigned long) gtheEvent.message >> 24) {    /**  high byte of message  **/
  243.                  case kSuspendResumeMessage:            /**  suspend/resume is also an activate/deactivate  **/
  244.                     if ((gtheEvent.message & kResumeMask) == 0)
  245.                        sleep = 80;                    /** we are headed to the background, so slow down...  **/
  246.                     else
  247.                        sleep = 0;                    /** we are headed to the foreground, so speed up...  **/
  248.                 break;
  249.             }
  250.                 break;
  251.     }
  252.     return true;
  253. }
  254.  
  255.